package com.itheima.imclient100.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.itheima.imclient100.BaseActivity;
import com.itheima.imclient100.R;
import com.itheima.imclient100.adapter.AddFriendAdapter;
import com.itheima.imclient100.presenter.AddFriendPresenter;
import com.itheima.imclient100.presenter.impl.AddFriendPresenterImpl;
import com.itheima.imclient100.utils.ToastUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by fullcircle on 2017/7/3.
 */

public class AddFriendActivity extends BaseActivity implements AddFriendView {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tb_toolbar)
    Toolbar tbToolbar;
    @InjectView(R.id.iv_nodata)
    ImageView ivNodata;
    @InjectView(R.id.rv_addfriend)
    RecyclerView rvAddfriend;
    @InjectView(R.id.fl_container)
    FrameLayout flContainer;
    @InjectView(R.id.activity_main)
    LinearLayout activityMain;

    AddFriendPresenter presenter = null;
    private AddFriendAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        ButterKnife.inject(this);
        initToolbar();
        presenter = new AddFriendPresenterImpl(this);
    }

    private void initToolbar() {
        tbToolbar.setTitle("");
        setSupportActionBar(tbToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_friend,menu);
        //getItem传入一个索引
        MenuItem item = menu.getItem(0);
        final SearchView actionView = (SearchView) item.getActionView();
        //设置输入框的提示文字
        actionView.setQueryHint("输入好友昵称搜索好友");
        //设置查询文字的监听
        actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //当点击回车 提交文字之后 会执行这个方法
                presenter.searchFriend(query);
                if(adapter == null){
                adapter = new AddFriendAdapter(null,null);
                  rvAddfriend.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                    rvAddfriend.setAdapter(adapter);
                    adapter.setOnAddFriendClickListener(new AddFriendAdapter.onAddFriendClickListener() {
                        @Override
                        public void onAddFriendClick(View v, String username) {
                            presenter.addFriend(username);
                        }
                    });
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //当输入的文本发生变化的时候 会走这个方法
                showToast(newText);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onGetSearchResult(boolean isSuccess, List<AVUser> users, List<String> username, String errMsg) {
        if(isSuccess){
            adapter.setUsers(users);
            adapter.setContacts(username);
            adapter.notifyDataSetChanged();

            ivNodata.setVisibility(View.GONE);
            rvAddfriend.setVisibility(View.VISIBLE);
        }else{
            showToast(errMsg);
            ivNodata.setVisibility(View.VISIBLE);
            rvAddfriend.setVisibility(View.GONE);
        }
    }

    @Override
    public void onGetAddFriendResult(boolean isSuccess, String errMsg) {
        if(isSuccess){
            showToast("添加好友请求成功发送到服务器");
        }else{
            showToast("添加好友请求失败");
        }
    }
}
